// page/wheat//pages/anchor_budang/anchor_budang.js
var app = getApp();
var util = app.util;
var maiList = app.http.anchor_mailistPost; //接口 排麦列表
var maiType = app.http.maiTypePost; //接口 排麦类型
var maiSubmit = app.http.savePaimaiPost; //接口 提交排麦
var fillMic = app.http.fill_micPost; //接口 补档
var setmaiOut;  //定时器
Page({

  /**
   * 页面的初始数据
   */
  data: {
     tab1Index: 0,                 //tab 切换
     is_onshow:1,    // 1调用onshow方法
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

      //计算scrooll的 高度px
      let headHeightpx =  120*wx.getSystemInfoSync().windowWidth/750;
      let maxscrollheight = wx.getSystemInfoSync().windowHeight - headHeightpx
      this.setData({
        maxscrollheight
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    //paimai列表
    this.getmaiList();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
    var dangqiInfo = wx.getStorageSync('dangqiInfo')
    if(!dangqiInfo){
      wx.switchTab({
        url: "/page/tabBar/wheat/index"
      })
      return;
    }
    //档期结束则跳到首页
    var endtime = wx.getStorageSync('dangqiInfo').riqi + " " + wx.getStorageSync('dangqiInfo').endtime
    if ((new Date().getTime() > util.riqiTime(endtime))) {
      if(!dangqiInfo){
        wx.switchTab({
          url: "/page/tabBar/wheat/index"
        })
        return;
      }
    }
    //页面标题为路由参数
    var beteenTime = dangqiInfo.starttime + " — " + dangqiInfo.endtime
    wx.setNavigationBarTitle({
      title: beteenTime
    })
    this.setData({
      dangqiInfo: dangqiInfo
      , beteenTime
    })
    //paimai列表
    setmaiOut = setInterval(function () {
      this.getmaiList();
    }.bind(this), "2000")
    //paimai类型
    if(this.data.is_onshow){
      this.getmaiType();
    }


    //打卡组件
    var mydaka = this.selectComponent("#my-daka")
    mydaka.showjiekou(); //初始化方法

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearInterval(setmaiOut);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(setmaiOut);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return util.shareSet(this, "补档");
  },
  //获取排麦列表
  getmaiList: function () {
    var dangqiInfo = wx.getStorageSync('dangqiInfo')
    var token = wx.getStorageSync('userinfo').token
    var hall_id = wx.getStorageSync('hall_info').id
    maiList({
      token,
      hall_id,
      dangqiid: dangqiInfo.id
    }, false).then(re => {
      //console.log(re);
      if (re.code == 0) {
        var my_uid = wx.getStorageSync('userinfo').uid
        var onList = false //是否是主播
        //获取跳档的个数和补档的个数   相等跳到已排页面
        var tiao = 0;
        var bu = 0;
        for (var i = 0; i < re.data.length; i++) {
          if (re.data[i].tripstop == 1) {
            tiao++;
          }
          if (re.data[i].addin == 1) {
            bu++;
          }
          if (re.data[i].uid == my_uid) {
            onList = true
          }
        }
        


        let isHost = 0
        
        if (my_uid == re.zhuchi_info.manager_id) {
          isHost = 1
        }
        //档期结束时间
        var endtime = util.riqiTime(dangqiInfo.riqi + " " + dangqiInfo.endtime)
      
        if (Number(re.addinbtn) == 0 || new Date().getTime() > endtime) {
          //主播 ，管理员， 主持
          if (isHost || wx.getStorageSync("userinfo").roles.indexOf("1") != -1 || onList){
            wx.redirectTo({
              url: '../main_daka/main_daka',
            })
          }else{
            wx.redirectTo({
              url: '../anchor_yipai/anchor_yipai',
            })
          }
        }
       

        this.setData({
          budangNum: bu,
          tiao: tiao,
          maiList: re.data,
          zhuchi_info: re.zhuchi_info,
          myuid: re.myuid,
          mypaimai: re.mypaimai,
          isHost,
          getpaimaiList:re
        })

      } else {
        console.log("接口返回数据失败", re);
      }
    })
  },
  //获取排麦类型
  getmaiType: function () {
    var hall_info = wx.getStorageSync('hall_info')
    var token = wx.getStorageSync('userinfo').token
    maiType({
      token,
      hall_id: hall_info.id
    }).then(re => {
      console.log(re);
      if (re.code == 0) {
        this.setData({
          maiType: re.data
        })
      } else {
        console.log("接口返回数据失败", re);
      }
    })
  },
  //选中类型
  setSueType: function (e) {
    var id = e.currentTarget.dataset.id;
    this.setData({
      xuanzhong: id
    })
  },
  //提交 补档
  submitPaimai: function () {
    var hall_info = wx.getStorageSync('hall_info')
    var token = wx.getStorageSync('userinfo').token
    var qid = wx.getStorageSync('dangqiInfo').id
    var tid = this.data.xuanzhong
    var that = this
    if (!tid) {
      wx.showToast({
        title: '请选择普通排',
        icon: 'none',//当icon：'none'时，没有图标 只有文字
        duration: 2000
      })
      return false
    }
    fillMic({
      token,
      tid,
      qid
    }).then(re => {
      console.log(re);
      if (re.code == 0) {
        //paimai列表
        that.getmaiList();
        //paimai类型
        that.getmaiType();
      } else {
        console.log("接口返回数据失败", re);
      }
    })
  },
   /*
   * tab 切换
   */
  onTab1Click(event) {
    let index = event.currentTarget.dataset.index;
    this.setData({
      tab1Index: index,
    })
  },
  /*
   *  swipe 切换
   */
  swiperChange(event) {
    this.setData({
      tab1Index: event.detail.current,
    })    
  },
  /*
   *  swipe 切换
   */
  animationfinish(event) {
    this.setData({
      tab1Index: event.detail.current,
    })
  },
  //打卡 福利弹框
  bottomDialog(e){
    var bottomDialog = this.selectComponent("#bottomDialog")
    bottomDialog.setData({
      hideFlag: false,
      selectArray:e.detail.bottomDialogArray,
      title:e.detail.bottomDialogTitle
    })
    bottomDialog.showModal();
  },
  //打卡 福利弹框回调
  bottomDialogIndex(e){
    var mydaka = this.selectComponent("#my-daka")
    mydaka.bottomDialogIndex(e) 
  },
  //打卡 大光弹框
  daguangDialog(e){

    var bottomDialog = this.selectComponent("#daguangDialog")
    bottomDialog.setData({
        show:e.detail.dialogDaguangShow,
        buttons:[{
          text: '取消'
          }, {
              text: '确定',
              extClass: 'daguangSure'
          }] //大光福利弹出框 按钮
    })
  },
  //打卡 大光弹框回调
  daguangSure(e){
    var mydaka = this.selectComponent("#my-daka")
    mydaka.daguangSure(e)
  },
  //打卡 大光弹框输入数字回调
  daguangNumber(e) {
    var mydaka = this.selectComponent("#my-daka")
    mydaka.daguangNumber(e)
  },
  //打卡 受益人
  shouyiren(e){
     this.setData({
      bottomindex:e.detail.bottomindex,
      userindex:e.detail.userindex,
      get_dakadata:e.detail.get_dakadata,
      isedit:e.detail.isedit,
      tid:e.detail.tid,
     })
  },
  //打卡 受益人页面调用的 方法
  addUserdaka(){
    var that = this;
    var mydaka = this.selectComponent("#my-daka")
    //黑麦
    var bottomindex = this.data.bottomindex;
    var dakadatabottomindex = this.data.get_dakadata[bottomindex] //
    if(dakadatabottomindex.type == 4){
      if (that.data.isedit == 0){//新增
        mydaka.setData({
          tid: dakadatabottomindex.benefitlist.id,
        })
      }
    }
    mydaka.setData({
      get_dakadata:that.data.get_dakadata,
      is_onshow: 0     // 不调用onshow 里的方法
    })
    mydaka.addUserdaka()
  },
  //打卡按钮
  showdelet(e){
    this.setData({
      showdelet:e.detail.showdelet
      })
  },
  //打卡
  godaka(){
    var mydaka = this.selectComponent("#my-daka")
    mydaka.godaka()
  },
})